home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: Taifun / Taifun 078 (1988-11-15)(Ossowski, Stefan)(DE)(PD).zip / Taifun 078 (1988-11-15)(Ossowski, Stefan)(DE)(PD).adf / Cluster / clusterIO.c < prev    next >
C/C++ Source or Header  |  1988-08-14  |  9KB  |  377 lines

  1. /*
  2.  * ClusterIO                                       © 1988 by Unicorn Systems
  3.  * ---------
  4.  * Sternhaufensimulation, Eingabe/Ausgabefunktionen
  5.  * Aztec C 3.6, Kickstart 1.2, Workbench 1.2,
  6.  * 
  7.  * Datum:   Autor:      Aenderung:                                   Version:
  8.  * --------------------------------------------------------------------------
  9.  * 1.8.88   Roger       Programm erstellt                            1.00
  10.  *
  11.  */
  12.  
  13. /*=========================   INCLUDES/DEFINES   ===========================*/
  14.  
  15. #include <exec/exec.h>
  16. #include <intuition/intuition.h>
  17. #include <intuition/intuitionbase.h>
  18. #include <graphics/display.h>
  19. #include <stdio.h>
  20. #include <clusterDEF.h>
  21. #include <clusterIO.h>
  22.  
  23.  
  24. /*==========================   MODULFUNKTIONEN   ===========================*/
  25.  
  26. void closeIO(), waitkey(), clear(), normalfarben(), pixel(), cleanup();
  27. int openIO(), getkey(), getword(), opendwin(), universum();
  28. void dclear(), dprint(), closedwin();
  29.  
  30.  
  31. /*============================   FUNKTIONEN   ==============================*/
  32.  
  33. /*
  34.  * Oeffnet das Datenfenster. Die Grösse dieses Fensters ist durch die Makros
  35.  * ZEILEN und SPALTEN definiert.
  36.  */
  37.  
  38. int opendwin()
  39. {
  40.    newdwin.Width = (dwidth = SPALTEN * 8 + 8);
  41.    newdwin.Height = (dheight = ZEILEN * 8 +17);
  42.    newdwin.Screen = scr;
  43.    if (! guru)
  44.       newdwin.TopEdge = 1;
  45.    if (dwin = OpenWindow(&newdwin)) {
  46.       dflag = 1;
  47.       dwinrp = dwin->RPort;
  48.       dwinvp = ViewPortAddress(dwin);
  49.       SetDrMd(dwinrp, JAM1);
  50.       clX = dwin->Width - dwin->BorderLeft - dwin->BorderRight;
  51.       clY = dwin->Height - dwin->BorderBottom - 1;
  52.       return(WAHR);
  53.    }
  54.    else
  55.       return(FALSCH);
  56. }
  57.  
  58.  
  59. /*
  60.  * Gibt im Datenfenster den globalen Zeichenvektor 'wort' an der Position
  61.  * 'zeile' und 'spalte' aus.
  62.  */
  63.  
  64. void dprint(zeile, spalte)
  65. register int zeile;
  66. register int spalte;
  67. {
  68.    SetAPen(dwinrp, 7L);
  69.    Move(dwinrp, (long) spalte * 8L + 5L, (long) zeile * 8L + 19L);
  70.    Text(dwinrp, wort, (long) strlen(wort));
  71. }
  72.  
  73.  
  74. /*
  75.  * Oeffnet alle Libraries sowie den Screen und das Hauptfenster, in dem 
  76.  * die Simulation stattfindet. Wenn das Programm auf einem PAL-Amiga läuft,
  77.  * wird ein Screen von 256 Pixeln höhe geöffnet, bei einem NTSC-Amiga 
  78.  * sollte die Höhe 200 Pixel sein (noch nicht getestet).
  79.  */
  80.  
  81. int openIO()
  82. {
  83.    fd = 0;
  84.    if ((IntuitionBase = OpenLibrary("intuition.library", REV)) == NULL)
  85.       return(FALSCH);
  86.    if ((GfxBase = OpenLibrary("graphics.library", REV)) == NULL)
  87.       return(FALSCH);
  88.    if ((ExecBase = OpenLibrary("exec.library", REV)) == NULL)
  89.       return(FALSCH);
  90.    /* Hier wird der erste Screen in der verketteten Liste bestimmt: */
  91.    scr = IntuitionBase->FirstScreen;
  92.    /* Suchen nach dem Workbench-Screen (Screentitel = Workbench): */
  93.    while (strncmp((char *) scr->Title, "Workbench", 9) != 0)
  94.       if (! (scr = scr->NextScreen))
  95.          return(FALSCH);               /* Workbenchscreen nicht gefunden */
  96.    if (scr->Height == 256 || scr->Height == 512)
  97.       height = 256;                    /* Wenn PAL-Amiga */
  98.    else
  99.       height = 200;                    /* Wenn NTSC-Amiga */
  100.    newscr.Height = height;
  101.    if (guru)
  102.       newwin.Height = height;
  103.    else {
  104.       newwin.Height = height - 1;
  105.       newwin.TopEdge = 1;
  106.    }
  107.    if (scr = OpenScreen(&newscr)) {
  108.       newwin.Screen = scr;
  109.       scrrp = &scr->RastPort;
  110.       scrvp = &scr->ViewPort;
  111.       if (win = OpenWindow(&newwin)) {
  112.          ShowTitle(scr, 0L);
  113.          winrp = win->RPort;
  114.          winvp = ViewPortAddress(win);
  115.          normalfarben();
  116.          SetDrMd(winrp, JAM1);
  117.          SetAPen(winrp, 13L);
  118.          return(WAHR);
  119.       } 
  120.    }
  121.    else {
  122.       CloseScreen(scr);
  123.       return(FALSCH);
  124.    }
  125. }
  126.  
  127.  
  128. /*
  129.  * Schliesst Eingabe- und Ausgabekanäle, gibt Speicher zurück. 
  130.  */
  131.  
  132. void closeIO()
  133. {
  134.    if (win) {
  135.       while (message = GetMsg(win->UserPort))
  136.          ReplyMsg(message);
  137.       CloseWindow(win);
  138.    }
  139.    closedwin();
  140.    if (scr)
  141.       CloseScreen(scr);
  142.    if (IntuitionBase)
  143.       CloseLibrary(IntuitionBase);
  144.    if (GfxBase)
  145.       CloseLibrary(GfxBase);
  146.    if (ExecBase)
  147.       CloseLibrary(ExecBase);
  148.    cleanup();
  149. }
  150.  
  151.  
  152. /*
  153.  * Schliesst das Datenfenster. Kann mehrmals aufgerufen werden.
  154.  */
  155.  
  156. void closedwin()
  157. {
  158.    if (dflag) {
  159.       CloseWindow(dwin);
  160.       dflag = 0;
  161.    }
  162. }
  163.  
  164.  
  165. /*
  166.  * Gibt den allozierten Speicher zurück.
  167.  */
  168.  
  169. void cleanup()
  170. {
  171.    register int i;
  172.    for (i = 0; i < 3; ++i) {
  173.       if (acc[i])
  174.          free(acc[i]);
  175.       if (vel[i])
  176.          free(vel[i]);
  177.       if (pos[i])
  178.          free(pos[i]);
  179.    }
  180.    if (col)
  181.       free(col);
  182.    if (mas)
  183.       free(mas);
  184.    if (fd)                    /* Kann offen sein nach 'universum()' */
  185.       fclose(fd);
  186. }
  187.  
  188.  
  189. /*
  190.  * Löscht das Hauptfenster, in dem die Simulation stattfindet.
  191.  */
  192.  
  193. void clear()
  194. {
  195.    SetAPen(winrp, 0L);
  196.    RectFill(winrp, 0L, 0L, (long) WIDTH, (long) height);
  197. }
  198.  
  199.  
  200. /*
  201.  * Löscht das Datenfenster (muss geöffnet sein).
  202.  */
  203.  
  204. void dclear()
  205. {
  206.    SetAPen(dwinrp, 0L);
  207.    RectFill(dwinrp, (long) (dwin->BorderLeft + 1), \
  208.             (long) (dwin->BorderTop + 1), (long) clX, (long) clY);
  209. }
  210.  
  211.  
  212. /*
  213.  * Wartet auf einen Tastendruck. 
  214.  */
  215.  
  216. void waitkey()
  217. {
  218.    for (;;) {
  219.       class = 0;
  220.       Wait(1L<<win->UserPort->mp_SigBit);
  221.       while (message = GetMsg(win->UserPort)) {
  222.          class = message->Class;
  223.          code = message->Code;
  224.          qual = message->Qualifier;
  225.          ReplyMsg(message);
  226.       }
  227.       if (class == VANILLAKEY)
  228.          break;
  229.    }
  230. }
  231.  
  232.  
  233. /*
  234.  * Gibt als Wert 1 zurück, wenn eine Taste gedrückt wurde. Wenn ein Event
  235.  * eingetreten ist, werden alle weiteren Events ausgelesen. (Kein Puffer!)
  236.  */
  237.  
  238. int getkey()
  239. {
  240.    message = (void *) 0;
  241.    class = 0;
  242.    while (message = GetMsg(win->UserPort)) {
  243.       class = message->Class;
  244.       code = message->Code;
  245.       qual = message->Qualifier;
  246.       ReplyMsg(message);
  247.    }
  248.    if (class == VANILLAKEY)
  249.       return(WAHR);
  250.    return(FALSCH);
  251. }
  252.  
  253.  
  254. /*
  255.  * Setzt den Punkt x, y mit der Farbe f. Die Koordinaten haben den Null-
  256.  * punkt in der Mitte des Bildschirms. Die Vorzeichen der Koordinaten:
  257.  *
  258.  *                           |+Y
  259.  *                           |
  260.  *                    -X ----0---- +X
  261.  *                           |
  262.  *                           |-Y
  263.  */
  264.  
  265. void pixel(x, y, f)
  266. register int x, y, f;
  267. {
  268.    SetAPen(winrp, (long) f);
  269.    WritePixel(winrp, x + WIDTH / 2L, height - (y + height / 2L));
  270. }
  271.  
  272.  
  273. /*
  274.  * Liest die Daten eines Sternhaufens mit dem Namen 's' ein. Wenn alles 
  275.  * verstanden wurde, ist der Wert = WAHR, sonst FALSCH. Format der Daten:
  276.  * 1. Anzahl Sterne, 2. Koordinaten x, y und z des ersten Sterns,
  277.  * 3. Geschwindigkeit x, y, z - Achse des ersten Sterns, 4. Masse des
  278.  * ersten Sterns, 5. Farbe von 0 bis 15 des ersten Sterns.
  279.  */
  280.  
  281. int universum(s)
  282. char *s;
  283. {
  284.    register int c, i, j;
  285.    double zahl;
  286.    if (! (fd = fopen(s, "r")))
  287.       return(FALSCH);                     /* File nicht lesbar. */
  288.    if (getword() == EOF)
  289.       return(FALSCH);
  290.    if ((stars = atoi(wort)) == 0)
  291.       return(FALSCH);
  292.    for (j = 0; j < 3; ++j) {
  293.       if (acc[j] = (double *) malloc(sizeof(double) * stars))
  294.          if (vel[j] = (double *) malloc(sizeof(double) * stars))
  295.             if (pos[j] = (double *) malloc(sizeof(double) * stars))
  296.                continue;
  297.       cleanup();
  298.       return(FALSCH);
  299.    }
  300.    if (! (col = (int *) malloc(sizeof(int) * stars))) {
  301.       cleanup();
  302.       return(FALSCH);
  303.    }
  304.    if (! (mas = (double *) malloc(sizeof(double) * stars))) {
  305.       cleanup();
  306.       return(FALSCH);
  307.    }
  308.    for (i = 0; i < stars; ++i) {
  309.       for (j = 0; j < 3; ++j)             /* Koordinaten */
  310.          if (getword() != EOF)
  311.             pos[j][i] = atof(wort);
  312.          else {
  313.             cleanup();
  314.             return(FALSCH);
  315.          }
  316.       for (j = 0; j < 3; ++j)             /* Geschwindigkeit */
  317.          if (getword() != EOF)
  318.             vel[j][i] = atof(wort);
  319.          else {
  320.             cleanup();
  321.             return(FALSCH);
  322.          }
  323.       if (getword() != EOF) {             /* Masse */
  324.          mas[i] = atof(wort);
  325.          if (getword() != EOF) {          /* Farbe */
  326.             col[i] = atoi(wort);
  327.             if (col[i] >= 0 && col[i] < COLORS)
  328.                continue;
  329.          }
  330.       }
  331.       cleanup();
  332.       return(FALSCH);
  333.    }
  334.    for (i = 0; i < stars; ++i)
  335.       for (j = 0; j < 3; ++j)
  336.          acc[j][i] = 0.0;                 /* Beschleunigungen auf Null */
  337.    fclose(fd);
  338.    return(WAHR);
  339. }
  340.  
  341.  
  342. /*
  343.  * Liest das nächste Wort in den Stringvektor 'wort' ein. Wenn kein Wort
  344.  * mehr vorhanden ist (EOF), ist die Rückgabe 'EOF', sonst 'NULL' 
  345.  */
  346.  
  347. int getword()
  348. {
  349.    register int c, i = 0;
  350.    while ((c = getc(fd)) != EOF)
  351.       if (c != ' ' && c != '\n' && c != '\t' && c != '\f')
  352.          wort[i++] = c;
  353.       else
  354.          if (i > 0)
  355.             break;
  356.    if (i > 0) {
  357.       wort[i] = STREND;
  358.       return(NULL);
  359.    }
  360.    return(EOF);
  361. }
  362.  
  363.  
  364. /*
  365.  * Setzt die Farben auf Werte der 'colortable'. 
  366.  */
  367.  
  368. void normalfarben()
  369. {
  370.    register int i;
  371.    for (i = 0; i < COLORS; ++i) 
  372.       SetRGB4(scrvp, (long) i, (long) colortable[i][0], \
  373.                                (long) colortable[i][1], \
  374.                                (long) colortable[i][2]);
  375. }
  376.  
  377.